package game.model.impl

import game.Config
import game.business.AttackAble
import game.business.BlockAble
import game.business.DestroyAble
import game.business.SufferAble
import game.model.View
import org.itheima.kotlin.game.core.Painter

/**
 * 大本营
 *
 * 阻挡功能
 * 被攻击功能
 * 销毁能力
 */
class Camp(override var x: Int, override var y: Int) : BlockAble, SufferAble, DestroyAble {
    override var blood: Int = Config.CampBlood

    override var height: Int = Config.block + 32

    override var width: Int = Config.block * 2

    override fun draw() {
        if (blood < 3) {
            width = Config.block
            height = Config.block
            x = (Config.gameWidth - Config.block) / 2
            y = Config.gameHeight - Config.block

            Painter.drawImage("img/camp.gif", x , y )
        } else if (blood < 12) {
            //绘制外围的砖块
            for (i in 0..3) {
                Painter.drawImage("img/wall_small.gif", x + i * 32, y)
            }

            Painter.drawImage("img/wall_small.gif", x, y + 32)
            Painter.drawImage("img/wall_small.gif", x, y + 64)
            Painter.drawImage("img/wall_small.gif", x + 96, y + 32)
            Painter.drawImage("img/wall_small.gif", x + 96, y + 64)

            Painter.drawImage("img/camp.gif", x + 32, y + 32)
        } else {
            //绘制外围的砖块
            for (i in 0..3) {
                Painter.drawImage("img/steel_small.gif", x + i * 32, y)
            }

            Painter.drawImage("img/steel_small.gif", x, y + 32)
            Painter.drawImage("img/steel_small.gif", x, y + 64)
            Painter.drawImage("img/steel_small.gif", x + 96, y + 32)
            Painter.drawImage("img/steel_small.gif", x + 96, y + 64)

            Painter.drawImage("img/camp.gif", x + 32, y + 32)
        }


    }

    override fun notifySuffer(attackAble: AttackAble): Array<View>? {
        blood -= attackAble.attackPower
        if (blood == 3 || blood == 6){
            return  arrayOf(Blast(x-32,y-32),
                    Blast(x,y-32),
                    Blast(x+Config.block-32,y-32),
                    Blast(x+Config.block,y-32),
                    Blast(x+Config.block*2-32,y-32),
                    Blast(x-32,y),
                    Blast(x-32,y + Config.block-32),
                    Blast(x-32,y + Config.block),
                    Blast(x + Config.block * 2-32,y),
                    Blast(x + Config.block * 2-32,y + Config.block - 32),
                    Blast(x + Config.block * 2-32,y + Config.block)
            )
        }
        return null
    }

    override fun isDestroyed(): Boolean  = blood <= 0
    override fun showDestroy(): Array<View>? {
        return arrayOf(
                Blast(x-32,y-32)
                ,Blast(x,y-32)
                ,Blast(x+32,y-32)

                ,Blast(x-32,y)
                ,Blast(x,y)
                ,Blast(x+32,y)

                ,Blast(x-32,y+32)
                ,Blast(x,y+32)
                ,Blast(x+32,y+32)
        )
    }

}